/*  header-comment
/*  file   : /Users/jessica/react-hrm-h5/src/actions/growth/index
/*  author :
/*  date   : 2017-6-4 18:27:59
/*  last   : 2017-11-0 18:14:23
*/
import { createAction } from "redux-actions";
import fetch from "../../utils/fetch.js";
import { set,get } from "../../utils/db";
import { setCacheData, getCacheData } from "../../utils/storeCache.js";

export const getUser = createAction("GROWTH_GET_USER");

export const fetchUser = ({ userId },showLoading) => async dispatch => {

  //当前登录人的userId
  const currentUserId = get("currentUserId");
  const getResumeRecordInfo = "/hrmregister/mobile/resumeRecord/getResumeRecordInfo";
  let cacheData = "";
  //只缓存当前人的信息
  if(currentUserId === userId){
    cacheData = await getCacheData(getResumeRecordInfo);
    //存在缓存则读取
    if(cacheData){
      dispatch(getUser(cacheData))
    }
  }
  let loading = true;
  //存在缓存
  if(!!cacheData){
    loading = false;
  }else if(showLoading){
    loading = true;
  }else{
    loading = false;
  }
  return fetch("/hrmregister/mobile/resumeRecord/getResumeRecordInfo", {
    method: "GET",
    meta: {
      dipId: 68541,
      showLoading: loading
    },
    params: {
      userId
    }
  }).then(res => {
    if (!res.success) return res;
    if(currentUserId === userId){
      setCacheData(getResumeRecordInfo, res.result);
    }
    dispatch(getUser(res.result));
    const resume = res.result.resumeRecordVPOs;

    set("corpId", resume && resume[0] && resume[0].corpId);
    return res;
  });
};

export const addHireTime = ({ date, userId, bizUuid }) => () =>
  fetch("/hrmregister/mobile/resumeRecord/addHireTime", {
    method: "POST",
    body: {
      hireTime: date,
      userId,
      bizUuid
    }
  });

export const handover = ({ userId }) => () =>
  fetch("/hrmregister/mobile/resumeRecord/informHandOverMessage", {
    method: "GET",
    params: {
      userId
    }
  });
export const regular = ({ userId }) => () =>
  fetch("/hrmregister/mobile/resumeRecord/informRegularMessage", {
    method: "GET",
    showLoading: false,
    params: {
      userId
    }
  });

export const doctor = ({ userId, type }) => () =>
  fetch(" /hrmregister/mobile/resumeRecord/getProcessInstanceStatusByType", {
    method: "POST",
    showLoading: false,
    body: {
      userId,
      type
    }
  });

export const leaveJob = ({
  userId,
  dismissionMemo,
  dismissionReason,
  lastWorkDate
}) => () =>
  fetch("/hrmregister/mobile/resumeRecord/doDisMission", {
    method: "POST",
    body: {
      userId,
      lastWorkDate,
      dismissionReason,
      dismissionMemo
    }
  });

export const deleteEmployee = ({ userId }) => () =>
  fetch("/hrmregister/mobile/empList/deletePreDimissiondEmp", {
    method: "GET",
    params: {
      userId
    }
  });
export const abort = ({ userId }) => () =>
  fetch("/hrmregister/mobile/resumeRecord/rollBackDismission", {
    method: "GET",
    params: {
      userId
    }
  });

export const getAllLeaveReasons = ({ userId }) => () =>
  fetch("/hrmregister/mobile/employeeDismission/getDismissionInfo", {
    method: "POST",
    body: {
      userId
    },
    meta: {
      dipId: 67772
    }
  });

export const getLeaveAndDeleteData = ({ userId }) => () =>
  fetch("/hrmregister/mobile/employeeDismission/getDismissionRemindInfo", {
    method: "POST",
    body: {
      userId
    },
    meta: {
      dipId: 69132
    }
  });

export const leaveJobAndDelete = body => () =>
  fetch("/hrmregister/mobile/employeeDismission/doDisMission", {
    method: "POST",
    body
  });

// export const getAllLeaveReasons = () => () =>
//   fetch("/hrmregister/mobile/resumeRecord/getDisMissionResonDropDown", {
//     method: "GET"
//   });
